package daxiniot.bttemphumidity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class HistoryActivity extends AppCompatActivity {
    private Button btn1;
    //依靠DatabaseHelper带全部参数的构造函数创建数据库
    private DatabaseHelper dbHelper;
    private SQLiteDatabase db;
     private ArrayList<DataBean> datalist;
    private ListView lv;
    private EditText querytime;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        //打开数据库
        dbHelper = new DatabaseHelper(HistoryActivity.this, "test_db",null,1);
        db = dbHelper.getWritableDatabase();

        querytime= (EditText) findViewById(R.id.query_time);
        btn1 = (Button) findViewById(R.id.btn_query);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String timeStr = querytime.getText().toString();

                if(TextUtils.isEmpty(timeStr))
                {
                    //在手机上输出
                    //Toast.LENGTH_SHORT:函数功能为显示时间短
                    //Toast.LENGTH_LONG :显示时间长
                    Toast.makeText(HistoryActivity.this, "请输入正确日期", Toast.LENGTH_SHORT).show();
                }
                else{

                    datalist = new ArrayList<>();
                    //扫描数据库,将数据库信息放入studentlist
                    Cursor cursor = db.rawQuery("select * from user_data where time=?",new String[] {
                            timeStr});
                    while (cursor.moveToNext()){
                        int id = cursor.getInt(cursor.getColumnIndex("id"));
                        String time = cursor.getString(cursor.getColumnIndex("time"));
                        String clock = cursor.getString(cursor.getColumnIndex("clock"));
                        int tem = cursor.getInt(cursor.getColumnIndex("temperature"));
                        int hum = cursor.getInt(cursor.getColumnIndex("humidity"));

                        DataBean databean;   //student_info存一个条目的数据
                        databean = new DataBean(id,time,clock,tem, hum);
                        datalist.add(databean);//把数据库的每一行加入数组中
                    }

                    //获取ListView,并通过Adapter把studentlist的信息显示到ListView
                    //为ListView设置一个适配器,getCount()返回数据个数;getView()为每一行设置一个条目
                    lv=(ListView)findViewById(R.id.data_lv);
                    lv.setAdapter(new BaseAdapter() {
                        @Override
                        public int getCount() {
                            return datalist.size();
                        }

                        //ListView的每一个条目都是一个view对象
                        @Override
                        public View getView(int position, View convertView, ViewGroup parent) {
                            View view;
                            //对ListView的优化，convertView为空时，创建一个新视图；convertView不为空时，代表它是滚出
                            //屏幕，放入Recycler中的视图,若需要用到其他layout，则用inflate(),同一视图，用fiindViewBy()
                            if(convertView==null){
                                view = View.inflate(getBaseContext(),R.layout.itemlayout,null);
                            }
                            else{
                                view = convertView;
                            }

                            //从studentlist中取出一行数据，position相当于数组下标,可以实现逐行取数据
                            DataBean databean = datalist.get(position);
                            TextView time = (TextView)view.findViewById(R.id.stu_number);
                            TextView tem = (TextView)view.findViewById(R.id.stu_name);
                            TextView hum = (TextView)view.findViewById(R.id.stu_age);
                            time.setText(databean.getClock());
                            tem.setText(String.valueOf(databean.getTemperature()));
                            hum.setText(String.valueOf(databean.getHumidity()));
                            return view;
                        }

                        @Override
                        public Object getItem(int position) {
                            return null;
                        }

                        @Override
                        public long getItemId(int position) {
                            return 0;
                        }
                    });





                    Toast.makeText(HistoryActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
                }

            }
        });

    }


    }


